﻿#region MyJScript - LGPL Copyright (c) 2008 Lionel Laské
//
// This file is part of MyJScript.
//
// MyJScript is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// MyJScript is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with MyJScript; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
//================================================================================
#endregion

using Microsoft.Scripting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyJScript.Runtime;


namespace MyJScript.Test
{
    /// <summary>
    ///This is a test class for MJSObjectTest and is intended
    ///to contain all MJSObjectTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MJSObjectTest
    {
        public static object tostring(object @this, params object[] args)
        {
            return @this.ToString();
        }

        /// <summary>
        ///A test for Set
        ///</summary>
        [TestMethod()]
        public void SetTest()
        {
            MJSObject o1 = new MJSObject();
            Assert.AreEqual("{}", o1.ToString());

            o1.Set("name", "Hello");
            Assert.AreEqual("{name:Hello}", o1.ToString());
            o1.Set("age", 37);
            Assert.AreEqual("{name:Hello, age:37}", o1.ToString());
            o1.Set("tostring", Microsoft.Scripting.Utils.ReflectionUtils.CreateDelegate(typeof(MJSObjectTest).GetMethod("tostring"), typeof(MyJScriptCallTarget)));
            Assert.AreEqual("{name:Hello, age:37, tostring:MyJScript.Runtime.MyJScriptCallTarget}", o1.ToString());
            o1.Set("age", 43);
            Assert.AreEqual("{name:Hello, age:43, tostring:MyJScript.Runtime.MyJScriptCallTarget}", o1.ToString());

            MJSObject o2 = new MJSObject();
            o2.Set("x", 1);
            o2.Set("y", 2);
            Assert.AreEqual("{x:1, y:2}", o2.ToString());

            o1.Set("point", o2);
            Assert.AreEqual("{name:Hello, age:43, tostring:MyJScript.Runtime.MyJScriptCallTarget, point:{x:1, y:2}}", o1.ToString());
        }
        
        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void GetTest()
        {
            MJSObject o1 = new MJSObject();
            Assert.IsNull(o1.Get("name"));

            o1.Set("name", "Hello");
            Assert.AreEqual("Hello", o1.Get("name").ToString());
            o1.Set("age", 37);
            Assert.AreEqual("Hello", o1.Get("name").ToString());
            Assert.AreEqual("37", o1.Get("age").ToString());
            o1.Set("tostring", Microsoft.Scripting.Utils.ReflectionUtils.CreateDelegate(typeof(MJSObjectTest).GetMethod("tostring"), typeof(MyJScriptCallTarget)));
            Assert.AreEqual("Hello", o1.Get("name").ToString());
            Assert.AreEqual("37", o1.Get("age").ToString());
            Assert.AreEqual("MyJScript.Runtime.MyJScriptCallTarget", o1.Get("tostring").ToString());           

            o1.Set("age", 43);
            Assert.AreEqual("Hello", o1.Get("name").ToString());
            Assert.AreEqual("43", o1.Get("age").ToString());
            Assert.AreEqual("MyJScript.Runtime.MyJScriptCallTarget", o1.Get("tostring").ToString());           
            MJSObject o2 = new MJSObject();
            o2.Set("x", 1);
            o2.Set("y", 2);
            Assert.AreEqual("1", o2.Get("x").ToString());
            Assert.AreEqual("2", o2.Get("y").ToString());

            o1.Set("point", o2);
            Assert.AreEqual("Hello", o1.Get("name").ToString());
            Assert.AreEqual("43", o1.Get("age").ToString());
            Assert.AreEqual("MyJScript.Runtime.MyJScriptCallTarget", o1.Get("tostring").ToString());           
            Assert.AreEqual("{x:1, y:2}", o1.Get("point").ToString());
        }
    }
}
